package com.imocc.controller;

import com.imocc.VO.ProductInfoVO;
import com.imocc.VO.ProductVO;
import com.imocc.VO.ResultVO;
import com.imocc.dataobject_PO.ProductCategory;
import com.imocc.dataobject_PO.ProductInfo;
import com.imocc.service.CategoryService;
import com.imocc.service.ProductService;
import com.imocc.util.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author 王志鹏
 * @Date 2019/3/3 15:15
 * 买家商品
 **/
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/listTest")
    public ResultVO list_Test() {
        ResultVO resultVO = new ResultVO();
        ProductVO productVO = new ProductVO();
        ProductInfoVO productInfoVO = new ProductInfoVO();

        productVO.setProductInfoVOList(Arrays.asList(productInfoVO));
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        resultVO.setData(productVO);

        return resultVO;
    }

    @GetMapping("/list")
    public ResultVO lists() {
        //1. 查询所有上加商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2. 查询类目(一次性查询类目)
        List<Integer> categoryTypeList = new ArrayList<>();
        //传统方法
        for (ProductInfo productInfo : productInfoList) {
            categoryTypeList.add(productInfo.getCategoryType());
        }
        //精简方法(java8, lambda) 反正我对 lambda 深恶痛绝!!!
//        List<Integer> categoryTypeList_S =  productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
//        productService.findB
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        //3. 数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory:productCategoryList) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());


            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for(ProductInfo productInfo:productInfoList){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }


//        ResultVO resultVO = new ResultVO();
//
//        resultVO.setCode(0);
//        resultVO.setMsg("成功");
//        resultVO.setData(productVOList);

        return ResultVOUtil.success(productVOList);
    }
}
